package com.lancoo.znbkxx.views;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lancoo.znbkxx.R;


/**
 * Create by TangJun on 2019/2/16
 */
public class AppLoadingDialog extends Dialog {
    private Boolean cancelable = false;
    private int backGround = -1;
    private Context context;
    private LoadingView  loadingView;

    public AppLoadingDialog(Context context, Boolean cancelable, int backGround) {
        this(context, cancelable);
        this.backGround = backGround;
    }

    public AppLoadingDialog(Context context, Boolean cancelable) {
        super(context);
        this.context = context;
        this.cancelable = cancelable;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除title
        super.onCreate(savedInstanceState);

        setContentView(View.inflate(getContext(), getLayout(), null));
        loadingView =  findViewById(R.id.loading_pic);
        if (backGround != -1) {
            loadingView.setBackgroundResource(backGround);
        }
        Window window = this.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00ffffff")));
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
        setCancelable(cancelable);
        setOnShowListener(showListener);
    }

    public int getLayout(){
        return R.layout.view_app_loading_dialog;
    }


    private OnShowListener showListener = new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {

        }
    };


    @Override
    public void setOnDismissListener(final @Nullable DialogInterface.OnDismissListener listener) {
        super.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                listener.onDismiss(dialogInterface);
            }
        });
    }



}
